﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;
using Echo.Core.Common;

////*************************************************************************************************************************************
////Created by: Jannette Hamilton
////Date created: November 6, 2013
////Modified by: 
////Date modified: 
////Reason: CONVERTED FROM SWAT: SuiteCompliance.SuiteDotNet.SuiteCompanyTaxSetup.SuiteGuiTests.TestBusRuleTaxCodes.TestBusRuleTaxCodesReportingInformation
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules.Taxes
{
    [TestFixture]

    [Author("Jannette Hamilton", "Jannette_Hamilton@ultimatesoftware.com")]
    [Category("<Business Rules>")]
    [Header("Config")]
    [Config(".NET")]
    //[Config("IEOnly")]
    //[Config("ChromeOnly")]
    //[Config("FFOnly")]

    public class TaxCodeReportingInformation : MbUnitAutomatedTest
    {
        UltiPro UltiPro;
        UltiProDatabaseContext Database;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("UltiPro_CALENDAR");
            Database.Company.AddRollBack("RbsBusRuleRole", "rbrBusinessRule in ('TAXCODE')");
            Database.Company.AddRollBack("TaxCode", "ctcCoid = 'BVEBW'");
            Database.Company.Execute("DECLARE @roleID int;SET @roleID = (SELECT rolID from RbsRoles where rolName = 'SUEPRGTWYSYSADM');delete from rbsBusRuleRole where rbrBusinessRule = 'TAXCODE' AND rbrRoleID = @roleID;Insert into RbsBusRuleRole (rbrBusinessRule, RbrRightsMask, rbrRoleID, rbrType) Values ('TAXCODE',6,@roleID,'B');");
            Database.Company.Execute("update taxcode set ctcIdNumber = NULL where ctcTaxCode = 'FLSUIER' AND ctcCoid = 'BVEBW'");
            UltiPro.Pages.Login.LoginAs("JenkinsB", "password", Config.LanguageCode);
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            //Database.Company.RollBackDatabase(); // dbo.TaxCode' is not updatable because the modification affects multiple base tables
            UltiPro.Browser.Quit();
        }

        [SetUp]
        public void SetUp()
        {
           UltiPro.Macros.Navigate.ToBusinessRule("Taxes");
        }

        [TearDown]
        public void TearDown()
        {
        }

        #region Helper Functions
        public void SelectTaxCode(string sTaxCode)
        {
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(searchFilter => searchFilter.CodeFilter, FilterType.Is, sTaxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(grid => grid.Code, sTaxCode).Click(column => column.Code);
        }
        #endregion

        [Test(Order = 1)]
        public void VerifyNoSpecCharInEmployerIdField()
        {
            //VERIFY THAT EMPLOYER ID FIELD DOES NOT ACCEPT SPECIAL CHARACTERS
            SelectTaxCode("FLSUIER");
            UltiPro.Pages.TaxCodeContributionRate.Is(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.View_slash_Change_Taxes.ToString());
            UltiPro.Pages.TaxCodeContributionRate.Is(textBox => textBox.EmployerIdNumberTextBox, "innerHtml", string.Empty);
            UltiPro.Pages.TaxCodeContributionRate.Set(textBox => textBox.EmployerIdNumberTextBox,  "/@#$%^&*()_+\\");
            UltiPro.Pages.TaxCodeContributionRate.Is(textBox => textBox.EmployerIdNumberTextBox, "innerHtml", string.Empty);
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
        }

        [Test(Order = 2)]
        public void VerifyBlankEmployerIdField()
        {
            // VERIFY THAT ER ID FIELD IS BLANK
            SelectTaxCode("FLSIT");
            UltiPro.Pages.TaxCodeContributionRate.Is(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.View_slash_Change_Taxes.ToString());
            UltiPro.Pages.TaxCodeContributionRate.Is(textBox => textBox.EmployerIdNumberTextBox, "innerHtml", string.Empty);
        }

        [Test(Order = 3)]
        public void VerifyPopulatedEmployerIdField()
        {
            //VERIFY VALUE IN POPULATED ER ID FIELD
            SelectTaxCode("HISUIER");
            UltiPro.Pages.TaxCodeContributionRate.Is(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.View_slash_Change_Taxes.ToString());
            UltiPro.Pages.TaxCodeContributionRate.Is(textBox => textBox.EmployerIdNumberTextBox, "value", "88-143590000000");
        }

        [Test(Order = 4)]
        public void VerifySpecCharInEmployerIdFieldMessage()
        {
            // GIVEN THAT ER ID WITH SPECIAL CHARS WAS ENTERED IN BACK OFFICE
            SelectTaxCode("OH1310");
            UltiPro.Pages.TaxCodeContributionRate.Is(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.View_slash_Change_Taxes.ToString());
            UltiPro.Pages.TaxCodeContributionRate.Like(textBox => textBox.EmployerIdNumberTextBox, "value", "123!@#$%$#$%");

            //WHEN USER TRIES TO SAVE THE TAX CODE WITH SPECIAL CHARS ON .NET
            UltiPro.Pages.TaxCodeContributionRate.PressKeyboardKey(textBox => textBox.EmployerIdNumberTextBox, KeyboardKey.End);
            UltiPro.Pages.TaxCodeContributionRate.PressKeyboardKey(textBox => textBox.EmployerIdNumberTextBox, KeyboardKey.Backspace);
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(button => button.Save);

            //THEN THIS ERROR MESSAGE IS DISPLAYED
            UltiPro.Pages.TaxCodeContributionRate.ErrorMessages.Has("Employer Id Number can only contain letters, numbers, a period, or a dash. EmployerID");
        }

        [Test(Order = 5)]
        public void SelectNoneInW2Box14()
        {
            SelectTaxCode("HISIT");

            //GIVEN THAT Z IS SELECTED IN W2 BOX 14 LABEL FIELD; WHEN THE CHANGES ARE SAVED...
            UltiPro.Pages.TaxCodeContributionRate.Is(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.View_slash_Change_Taxes.ToString());
            UltiPro.Pages.TaxCodeContributionRate.Set(dropDown => dropDown.W2Box14DropDown, "Z", SelectOption.Value);
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(button => button.Save);

            //THEN THE GUIS IS UPDATED
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(row => row.Code, "HISIT", col => col.W2Box14Label, "<None>").RowExists();
            SelectTaxCode("HISIT");
            UltiPro.Pages.TaxCodeContributionRate.Is(header => header.W2Box14DropDown, "innerHTML", UltiPro.Localization.Strings.None.ToString());

            //THEN THE DB IS UPDATED
            Database.Company.Verify("select CtcW2Box14Label from taxcode where ctctaxcode = 'HISIT' and ctccoid = 'BVEBW' and ctcw2box14label is NOT NULL", "TaxSetup/TaxCodeReportingInfo_W2Box14None.xml");
        }

        [Test(Order = 6)]
        public void VerifyNoneInW2Box14ThenSelectBlank()
        {
            SelectTaxCode("HISIT");

            //GIVEN THAT BLANK IS SELECTED IN W2 BOX 14 LABEL FIELD; WHEN THE CHANGES ARE SAVED...
            UltiPro.Pages.TaxCodeContributionRate.Set(dropDown => dropDown.W2Box14DropDown, "", SelectOption.Value);
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(button => button.Save);

            //THEN THE GUI IS UPDATED
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(row => row.Code, "HISIT", col => col.W2Box14Label, "").RowExists();

            //THEN THE DB IS UPDATED
            Database.Company.Verify("select CtcW2Box14Label from taxcode where ctctaxcode = 'HISIT' and ctccoid = 'BVEBW' and ctcw2box14label is NULL", "TaxSetup/TaxCodeReportingInfo_W2Box14Blank.xml");
        }
    }
}
